package com.spider.admin.conf.datasources;


import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
@Slf4j
@Configuration
@MapperScan(
        basePackages = "com.spider.admin.store.primary",
        sqlSessionFactoryRef = "sqlSessionFactoryPrimary",
        sqlSessionTemplateRef = "sqlSessionTemplatePrimary")
public class PrimaryDataSourceConfig {

    @Value("${spring.datasource.primary.druid.jdbcUrl}")
    private String url;
    @Value("${spring.datasource.primary.druid.username}")
    private String username;
    @Value("${spring.datasource.primary.druid.password}")
    private String password;
    @Value("${spring.datasource.primary.druid.driver-class-name}")
    private String driverClassName;
    @Value("${spring.datasource.primary.druid.initial-size}")
    private int initialSize;
    @Value("${spring.datasource.primary.druid.min-idle}")
    private int minIdle;
    @Value("${spring.datasource.primary.druid.max-active}")
    private int maxActive;
    @Value("${spring.datasource.primary.druid.max-wait}")
    private int maxWait;
    @Value("${spring.datasource.primary.druid.validationQuery}")
    private String validationQuery;

    @Primary
    @Bean(name = "primaryDataSource")
    public DataSource primaryDataSource() {
//        DataSource dataSource=DataSourceBuilder.create().build();
//        log.info("完成Primary数据源初始化");
//        return dataSource;

        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(url);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);

        //configuration
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);
        datasource.setMaxActive(maxActive);
        datasource.setMaxWait(maxWait);
//        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
//        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        datasource.setValidationQuery(validationQuery);
//        datasource.setTestWhileIdle(testWhileIdle);
//        datasource.setTestOnBorrow(testOnBorrow);
//        datasource.setTestOnReturn(testOnReturn);
//        datasource.setPoolPreparedStatements(poolPreparedStatements);
//        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
//        try {
//            datasource.setFilters(filters);
//        } catch (SQLException e) {
//            System.err.println("druid configuration initialization filter: " + e);
//        }
//        datasource.setConnectionProperties(connectionProperties);
        log.info("完成Primary数据源初始化");
        return datasource;
    }


    @Bean
    public SqlSessionFactory sqlSessionFactoryPrimary(@Qualifier("primaryDataSource") DataSource primaryDataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(primaryDataSource);
        return bean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplatePrimary(@Qualifier("sqlSessionFactoryPrimary") SqlSessionFactory sqlSessionFactoryPrimary) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryPrimary);
    }
}
